#include "chatlistitem.h"
#include "ui_chatlistitem.h"
#include "utils.h"
#include <QTimeZone>

ChatListItem::ChatListItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatListItem)
{
    ui->setupUi(this);
    ui->red_point->hide();
}

ChatListItem::~ChatListItem()
{
    delete ui;
}

QSize ChatListItem::sizeHint() const
{
    return QSize(250, 70); // 返回自定义的尺寸
}



//void ChatListItem::SetAvatar(QString icon)
//{
//    QPixmap pixmap(icon);
//    ui->icon->setPixmap(pixmap.scaled(ui->icon->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
//    ui->icon->setScaledContents(true);
//}

void ChatListItem::SetInfo(int id,QString msgId,QString name,QString email, QString avatar)
{

    // 加载图片
//    QPixmap pixmap(icon);
    // 设置图片自动缩放
//    ui->red_point->setPixmap(pixmap_point.scaled(ui->red_point->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
//    ui->red_point->setScaledContents(true);
//    ui->icon->setPixmap(pixmap.scaled(ui->icon->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
//    ui->icon->setScaledContents(true);

    user_id = id;
    user_msgId = msgId;
    user_name = name;
    user_email = email;
    user_avatar = avatar;
    ui->name_lb->setText(name);
}

QStringList ChatListItem::GetInfo()
{
    QStringList friendInfo;
    friendInfo<<QString::number(user_id)<<user_msgId<<user_name<<user_email<<user_avatar;
    return friendInfo;
}

void ChatListItem::setAvatar(QString fileName)
{

    QPixmap pixmap;
    if (pixmap.loadFromData(readBytesFile(AVATAR_PATH,fileName)))
    {
        ui->avatar->setPixmap(pixmap.scaled(ui->avatar->size(),
                                           Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    else
    {
        qDebug() << "ChatListItem::setAvatar函数加载图片数据失败！";
    }
}

void ChatListItem::setTime(double nowTime)
{
    // 将秒级时间戳转换为毫秒级时间戳
    quint64 timeInMs = nowTime * 1000;
    QDateTime target = QDateTime::fromMSecsSinceEpoch(timeInMs);

    // 获取本地时区
    QTimeZone localTimeZone = QTimeZone::systemTimeZone();
    // 转换为本地时间
    target = target.toTimeZone(localTimeZone);

    QDateTime now = QDateTime::currentDateTime();
    // 获取今天的开始时间
    QDateTime todayStart = now.date().startOfDay();
    // 获取昨天的开始时间
    QDateTime yesterdayStart = todayStart.addDays(-1);

    if (target >= todayStart) {
        // 如果是今天，显示小时和分钟
        ui->time_lb->setText(target.toString("hh:mm"));
    } else if (target >= yesterdayStart) {
        // 如果是昨天，显示“昨天”
        ui->time_lb->setText("昨天");
    } else if (target.date().weekNumber() == now.date().weekNumber()) {
        // 如果是本周内（非今天和昨天），显示星期几
        QStringList weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        ui->time_lb->setText(weekDays[target.date().dayOfWeek() % 7]);
    } else {
        // 如果不是本周，显示年-月-日
        ui->time_lb->setText(target.toString("yyyy-MM-dd"));
    }
}



void ChatListItem::ShowRedPoint(int newMsgNums)
{
    //如果新消息为0则隐藏
    user_newMsgNums += newMsgNums;
    if(user_newMsgNums != 0){
        if(user_newMsgNums>99){
            ui->red_point->setText("99+");
            ui->red_point->show();
        }
        else{
            ui->red_point->setText(QString::number(user_newMsgNums));
            ui->red_point->show();
        }
    }
    else {
        ui->red_point->hide();
    }
}

int ChatListItem::getNewMsgNums()
{
    return user_newMsgNums;
}

QString ChatListItem::getAvatar()
{
    return user_avatar;
}

double ChatListItem::getScore()
{
    return user_score;
}

QString ChatListItem::getMsgId()
{
    return user_msgId;
}

void ChatListItem::reSetNewMsgNums()
{
    user_newMsgNums = 0;
    ShowRedPoint(user_newMsgNums);
}

void ChatListItem::setMsgAndTime(QString msg, double nowTime)
{
    setMsg(msg);

    setTime(nowTime);
}

void ChatListItem::setMsg(QString msg)
{
    // 去除字符串中的换行符 \n 和 \r
    msg = msg.remove('\n');
    msg = msg.remove('\r');
    ui->mes_lb->setText(msg);
}



void ChatListItem::SetInfo(QString msg, int newMsgNums, double nowTime, double score)
{
    setMsg(msg);

    ShowRedPoint(newMsgNums);

    setTime(nowTime);

    user_score = score;
}
